package org.jeecg.modules.smg.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;


import lombok.extern.slf4j.Slf4j;


import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.smg.entity.SmgAreaconfigures;
import org.jeecg.modules.smg.entity.SmgConfigures;
import org.jeecg.modules.smg.entity.SmgUser;
import org.jeecg.modules.smg.entity.SmgUsercertificate;
import org.jeecg.modules.smg.service.ISmgAreaconfiguresService;
import org.jeecg.modules.smg.service.ISmgConfiguresService;
import org.jeecg.modules.smg.service.ISmgUserService;
import org.jeecg.modules.smg.service.ISmgUsercertificateService;
import org.jeecg.modules.smg.vo.AreaconfiguresVO;
import org.jeecg.modules.smg.vo.SmgConfiguresVO;
import org.jeecg.modules.smg.vo.SmgUserUpdateVO;
import org.jeecg.modules.smg.vo.SmgUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: smg_user
 * @Author: jeecg-boot
 * @Date: 2020-12-03
 * @Version: V1.0
 */
@Api(tags = "smg_user")
@RestController
@RequestMapping("/smg/smgUser")
@Slf4j
public class SmgUserController extends JeecgController<SmgUser, ISmgUserService> {
    @Autowired
    private ISmgUserService smgUserService;

    @Autowired
    private ISmgUsercertificateService iSmgUsercertificateService;

    @Autowired
    private ISmgConfiguresService iSmgConfiguresService;

    @Autowired
    private ISmgAreaconfiguresService areaconfiguresService;


//    @Autowired
//    private IsSys areaconfiguresService;

    /**
     * 分页列表查询
     *
     * @param smgUser
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "smg_user-分页列表查询")
    @ApiOperation(value = "smg_user-分页列表查询", notes = "smg_user-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SmgUser smgUser,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize,
                                   HttpServletRequest req) {

//        QueryWrapper<SmgUser> queryWrapper= null;
//        SmgUser user = new SmgUser();
//        if (smgUser.getType() == null) {
////            smgUser.setType("");
//            QueryGenerator.initQueryWrapper(user, req.getParameterMap());
//        }
//
        if (smgUser.getType() != null && smgUser.getType().equals("2")) {
            smgUser.setType("");
        }

        QueryWrapper<SmgUser> queryWrapper = QueryGenerator.initQueryWrapper(smgUser, req.getParameterMap());
        Page<SmgUser> page = new Page<SmgUser>(pageNo, pageSize);
        IPage<SmgUser> pageList = smgUserService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 添加
     *
     * @param smgUser
     * @return
     */
    @AutoLog(value = "smg_user-添加")
    @ApiOperation(value = "smg_user-添加", notes = "smg_user-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SmgUser smgUser) {
        smgUserService.save(smgUser);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smgUser
     * @return
     */
    @AutoLog(value = "smg_user-编辑")
    @ApiOperation(value = "smg_user-编辑", notes = "smg_user-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SmgUser smgUser) {
        smgUserService.updateById(smgUser);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "smg_user-通过id删除")
    @ApiOperation(value = "smg_user-通过id删除", notes = "smg_user-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        smgUserService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "smg_user-批量删除")
    @ApiOperation(value = "smg_user-批量删除", notes = "smg_user-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smgUserService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "smg_user-通过id查询")
    @ApiOperation(value = "smg_user-通过id查询", notes = "smg_user-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SmgUser smgUser = smgUserService.getById(id);
        if (smgUser == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(smgUser);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param smgUser
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmgUser smgUser) {
        return super.exportXls(request, smgUser, SmgUser.class, "smg_user");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmgUser.class);
    }


    // ##########################业务接口########################################

    /**
     * username
     * id
     *
     * @return
     */
    @RequestMapping(value = "/selectUser", method = RequestMethod.GET)
    public Result<?> selectUser() {
        List<SmgUserVO> collect = smgUserService.list(null).stream().map(e -> {
            SmgUserVO vo = new SmgUserVO()
                    .setId(e.getId())
                    .setUsername(e.getUsername());
            return vo;
        }).collect(Collectors.toList());
        return Result.ok(collect);
    }


    /**
     * 用户id
     *
     * @return
     */
    @RequestMapping(value = "/selectUid", method = RequestMethod.GET)
    public Result<?> selectUid(String uid) {
        QueryWrapper<SmgUsercertificate> wrapper = new QueryWrapper<SmgUsercertificate>();
        if (!StringUtils.isEmpty(uid)) {
            wrapper.eq("aid", uid);
        }

        List<SmgUsercertificate> list = iSmgUsercertificateService.list(wrapper);
        /**
         * 中间表查询 用户id  list
         * 根据用户id 去找到证件表
         */
        List<SmgConfiguresVO> id = list.stream().map(e -> {
            SmgConfiguresVO vo = new SmgConfiguresVO();  //返回结构
            vo.setMaxnumber(e.getMaxnumber());

            //去证件表查询
            SmgConfigures one = iSmgConfiguresService.getOne(new QueryWrapper<SmgConfigures>().eq("id", e.getCid()));
            vo.setConName(one.getConName());
            vo.setCid(e.getId());
            return vo;
        }).collect(Collectors.toList());

        return Result.ok(id);
    }


    /**
     * 证件配置
     *
     * @return
     */
    @RequestMapping(value = "/configUid", method = RequestMethod.GET)
    public Result<?> ConfigUid() {
        List<SmgConfigures> list = iSmgConfiguresService.list();
        List<SmgConfiguresVO> collect = list.stream().map(e -> {
            SmgConfiguresVO vo = new SmgConfiguresVO();
            vo.setConName(e.getConName())
                    .setCid(e.getId());
            return vo;
        }).collect(Collectors.toList());
        return Result.ok(collect);
    }


    /**
     * 证件配置
     *
     * @return
     */
    @PostMapping(value = "/AddConfig")
    public Result<?> AddConfig(@RequestBody SmgUsercertificate e) {
        if (!StringUtils.isEmpty(e.getCid()) && !StringUtils.isEmpty(e.getAid())) {
            //存在的情况
            QueryWrapper<SmgUsercertificate> wrapper = new QueryWrapper<SmgUsercertificate>();
            SmgUsercertificate one = iSmgUsercertificateService.getOne(new QueryWrapper<SmgUsercertificate>().eq("aid", e.getAid()).eq("cid", e.getCid()));
            if (one != null) {
                return Result.error("已存在");
            }

        }
        //不存在的情况
        iSmgUsercertificateService.save(e);

        return Result.ok();
    }


    /**
     * Regional search of certificates
     * 证件的 配置区域
     *
     * @return
     */
    @GetMapping(value = "/regional")
    public Result<?> Regional(@RequestParam String acid) {
        List<SmgAreaconfigures> acid1 = areaconfiguresService.list(new QueryWrapper<SmgAreaconfigures>().eq("acid", acid));
        List<AreaconfiguresVO> collect = acid1.stream().map(e -> {
            AreaconfiguresVO vo = new AreaconfiguresVO();
            vo.setId(e.getId())
                    .setAcid(e.getAcid())
                    .setArea(e.getArea())
                    .setMaxnumber(e.getMaxnumber());
            return vo;

        }).collect(Collectors.toList());

        return Result.ok(collect);
    }


    /**
     * 证件的数量配置编译
     *
     * @return
     */
    @GetMapping(value = "/regionalInfo")
    public Result<?> RegionalInfo(@RequestParam String acid) {
        AreaconfiguresVO vo = new AreaconfiguresVO();
        //中间表
        SmgUsercertificate id = iSmgUsercertificateService.getOne(new QueryWrapper<SmgUsercertificate>().eq("id", acid));

        SmgConfigures id1 = iSmgConfiguresService.getOne(new QueryWrapper<SmgConfigures>().eq("id", id.getCid()));
        vo.setMaxnumber(id.getMaxnumber());
        vo.setConName(id1.getConName());
        return Result.ok(vo);
    }


    /**
     * 证件的数量配置编译
     *
     * @return
     */
    @PostMapping(value = "/regionalUpdate")
    public Result<?> RegionalUpdate(@RequestBody SmgUserUpdateVO e) {
        SmgUsercertificate q = new SmgUsercertificate().setId(e.getId()).setMaxnumber(e.getMaxnumber());
        iSmgUsercertificateService.update(q, new QueryWrapper<SmgUsercertificate>().eq("id", q.getId()));
        return Result.ok();
    }


    /**
     * 区域数量配置编译
     *
     * @return
     */

    @PostMapping(value = "/addressNum")
    public Result<?> addressNum(@RequestBody AreaconfiguresVO e) {
        SmgAreaconfigures o = new SmgAreaconfigures().setAcid(e.getAcid()) .setArea(e.getArea()) .setMaxnumber(e.getMaxnumber());
        areaconfiguresService.save(o);
        return Result.ok();
    }


    /**
     * 区域数量配置详情
     *
     * @return
     */

    @GetMapping(value = "/updateInfo")
    public Result<?> updateInfo(String id) {
        SmgAreaconfigures id1 = areaconfiguresService.getOne(new QueryWrapper<SmgAreaconfigures>().eq("id", id));
        return Result.ok(id1);
    }


    /**
     * 编辑区域数量配置
     *
     * @return
     */

    @PostMapping(value = "/updateAreacon")
    public Result<?> updateAreacon(@RequestBody AreaconfiguresVO e) {
        SmgAreaconfigures v = new SmgAreaconfigures();
        v.setId(e.getId());
        v.setMaxnumber(e.getMaxnumber());
        areaconfiguresService.update(v, new QueryWrapper<SmgAreaconfigures>().eq("id", e.getId()));
        return Result.ok();
    }

}
